Skip to content

feat(common): async thread pool#387

Merged
sinui0 merged 10 commits into
devfrom
feat/async-thread-pool
May 20, 2026
Merged

feat(common): async thread pool#387
sinui0 merged 10 commits into
devfrom
feat/async-thread-pool

Conversation

@sinui0
Copy link
Copy Markdown
Collaborator

@sinui0 sinui0 commented May 5, 2026

Closes #371

Replaces the per-task thread-spawning model in mpz-common with a single shared work-stealing async executor. Each task is an async future that owns a deterministic I/O channel; idle workers steal across each other so a long-running task can't pin work on its creator's thread.

  • New Executor (built on crossbeam-deque injector + per-worker queues + async_task).
    ExecutorBuilder configures num_threads, prefix, and a custom worker spawn callback (std::thread::spawn by default; web_spawn::spawn on wasm).
  • try_join / try_join3 / try_join4 use futures::future::try_join* directly. Closure shape for join / try_join* / map is now for<'a> FnOnce(&'a mut Context) -> BoxFuture<'a, R>.
  • Context::map no longer takes a weight closure (load balancing is gone with the new executor).
  • Removed: Multithread/MultithreadBuilder/Spawn plumbing, mt/ and st.rs modules,
    load_balance.rs, and Counter.

@sinui0 sinui0 requested a review from th4s May 5, 2026 18:21
@sinui0
Copy link
Copy Markdown
Collaborator Author

sinui0 commented May 5, 2026

@th4s here is what I had in mind.

We should integration test this with tlsn and make sure there are no performance regressions. I would appreciate if you took over this PR

@th4s
Copy link
Copy Markdown
Contributor

th4s commented May 6, 2026

Yeah I will take over.

@th4s
Copy link
Copy Markdown
Contributor

th4s commented May 12, 2026

very nice 👍

I get some decent regression in the browser when testing with the tlsn harness: Seems to be a flat 0.3-0.4 seconds extra offset.

TLSNotary Benchmark Harness: Native (main: 040c6881...)
Running benchmarks from: bench.toml
Output will be written to: metrics.csv

[00:01:07] ████████████████████████████████████████ 18/18 Benchmarks complete
================================================================================
TLSNotary Benchmark Results
================================================================================

cable_mpc [mpc] (20 Mbps, 20ms latency, 1KB↑ 2KB↓):
  Median:  14.46s

cable_proxy [proxy] (20 Mbps, 20ms latency, 1KB↑ 2KB↓):
  Median:  1.59s

fiber_mpc [mpc] (100 Mbps, 15ms latency, 1KB↑ 2KB↓):
  Median:  3.62s

fiber_proxy [proxy] (100 Mbps, 15ms latency, 1KB↑ 2KB↓):
  Median:  0.93s
--------------------------------------------------------------------------------

TLSNotary Benchmark Harness: Native (feat/async-threadpool-integration: 6b0f0338...)
Running benchmarks from: bench.toml
Output will be written to: metrics.csv

[00:01:07] ████████████████████████████████████████ 18/18 Benchmarks complete
================================================================================
TLSNotary Benchmark Results
================================================================================

cable_mpc [mpc] (20 Mbps, 20ms latency, 1KB↑ 2KB↓):
  Median:  14.50s

cable_proxy [proxy] (20 Mbps, 20ms latency, 1KB↑ 2KB↓):
  Median:  1.63s

fiber_mpc [mpc] (100 Mbps, 15ms latency, 1KB↑ 2KB↓):
  Median:  3.61s

fiber_proxy [proxy] (100 Mbps, 15ms latency, 1KB↑ 2KB↓):
  Median:  0.96s
--------------------------------------------------------------------------------

TLSNotary Benchmark Harness: Browser (main: 040c6881...)
Running benchmarks from: bench.toml
Output will be written to: metrics.csv

[00:01:23] ████████████████████████████████████████ 18/18 Benchmarks complete
================================================================================
TLSNotary Benchmark Results
================================================================================

cable_mpc [mpc] (20 Mbps, 20ms latency, 1KB↑ 2KB↓):
  Median:  15.61s

cable_proxy [proxy] (20 Mbps, 20ms latency, 1KB↑ 2KB↓):
  Median:  2.06s

fiber_mpc [mpc] (100 Mbps, 15ms latency, 1KB↑ 2KB↓):
  Median:  4.82s

fiber_proxy [proxy] (100 Mbps, 15ms latency, 1KB↑ 2KB↓):
  Median:  1.35s
--------------------------------------------------------------------------------

TLSNotary Benchmark Harness: Browser (feat/async-threadpool-integration: 6b0f0338...)
Running benchmarks from: bench.toml
Output will be written to: metrics.csv

[00:01:26] ████████████████████████████████████████ 18/18 Benchmarks complete
================================================================================
TLSNotary Benchmark Results
================================================================================

cable_mpc [mpc] (20 Mbps, 20ms latency, 1KB↑ 2KB↓):
  Median:  16.09s

cable_proxy [proxy] (20 Mbps, 20ms latency, 1KB↑ 2KB↓):
  Median:  2.40s

fiber_mpc [mpc] (100 Mbps, 15ms latency, 1KB↑ 2KB↓):
  Median:  5.14s

fiber_proxy [proxy] (100 Mbps, 15ms latency, 1KB↑ 2KB↓):
  Median:  1.68s

@sinui0 sinui0 force-pushed the feat/async-thread-pool branch from b4c5323 to bfc024a Compare May 19, 2026 20:33
@sinui0 sinui0 changed the title Feat/async thread pool feat(common): async thread pool May 19, 2026
Drop pending runnables on shutdown so awaiters see cancellation rather
than hanging on a worker that has exited. Without this, runnables sitting
in the injector or local queues kept `Arc<Inner>` alive (via their
captured schedule closures), creating a cycle that prevented the executor
from ever freeing its memory.

- `Executor::shutdown` drains the global injector before unparking workers.
- Worker exit path drains its local queue.
- `schedule` checks shutdown and drops the runnable instead of pushing.
Copy link
Copy Markdown
Contributor

@th4s th4s left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, modulo clippy

no regression anymore: tlsnotary/tlsn#1151 (comment)

@sinui0 sinui0 merged commit 3c78547 into dev May 20, 2026
4 checks passed
@sinui0 sinui0 deleted the feat/async-thread-pool branch May 20, 2026 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Utilize thread pools

3 participants